//
//  VideoWindowTableViewCell.m
//  NewVideo
//
//  Created by Jhero on 15/11/8.
//  Copyright © 2015年 Jhero. All rights reserved.
//

#import "VideoWindowTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation VideoWindowTableViewCell

- (void)awakeFromNib {
    
//     = [NSURL URLWithString:@"http://v.jxvdy.com/sendfile/w5bgP3A8JgiQQo5l0hvoNGE2H16WbN09X-ONHPq3P3C1BISgf7C-qVs6_c8oaw3zKScO78I--b0BGFBRxlpw13sf2e54QA"];
    
//    AVAsset *avAsset = [AVAsset assetWithURL:videoUrl];
//    AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
}

- (void)setVideoUrl:(NSURL *)videoUrl
{
    _videoUrl = videoUrl;
    AVPlayer *player = [[AVPlayer alloc] initWithURL:_videoUrl];
    self.videoController = [[AVPlayerViewController alloc] init];
    self.videoController.player = player;
//    [self.videoController.player addObserver:self forKeyPath:@"status" options:0 context:nil];
    self.videoController.view.frame = self.VideoView.bounds;
    self.videoController.view.layer.frame = self.VideoView.bounds;
    [self.VideoView addSubview:self.videoController.view];
    self.videoController.delegate = self;
    [self.videoController.player play];

}

- (void)dealloc
{
    
    [self.VideoView removeFromSuperview];
    
    [self.videoController.player pause];
    [self.videoController.player replaceCurrentItemWithPlayerItem:nil];
    
    self.videoController.player = nil;
    [self.videoController.player.currentItem cancelPendingSeeks];
    [self.videoController.player.currentItem.asset cancelLoading];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.videoController.player) {
        AVPlayerStatus status = [change[NSKeyValueChangeNewKey] integerValue];
        if (status == AVPlayerStatusReadyToPlay) {
            [self.videoController.player play];
            [self.videoController.player removeObserver:self forKeyPath:@"status"];
        }
    }
    else {
//        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark AVPlayerViewControllerDelegate
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"playerViewControllerWillStartPictureInPicture");
}

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"playerViewControllerDidStartPictureInPicture");
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"failedToStartPictureInPictureWithError");
}

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"playerViewControllerWillStopPictureInPicture");
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    NSLog(@"restoreUserInterfaceForPictureInPictureStopWithCompletionHandler");
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
